Fix pad_input_tensors to pad batch to a multiple of num_processes#4093
Open
iamsharduld wants to merge 1 commit into
Open
Fix pad_input_tensors to pad batch to a multiple of num_processes#4093iamsharduld wants to merge 1 commit into
iamsharduld wants to merge 1 commit into
Conversation
_pad_input_tensors used to_pad = num_processes - (batch_size // num_processes), which is unrelated to the padding needed for even splitting: it padded batches that are already divisible (8 -> 10 for num_processes=4) and produced sizes that are frequently NOT a multiple of num_processes (e.g. 6 -> 9, 12 -> 13), so the padded batch still can't be chunked evenly (pippy_forward / ScheduleGPipe rely on this). The existing tests passed only because each (batch_size, num_processes) pair happened to coincide with the correct value. Use the standard pad-to-next-multiple formula: to_pad = (num_processes - batch_size % num_processes) % num_processes which is 0 when already divisible and always yields a multiple of num_processes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
pad_input_tensors(_pad_input_tensors) computes the padding as:which is unrelated to the padding needed to make the batch splittable across processes. It:
batch_size=8, num_processes=4→ 10), andnum_processes(e.g.6 → 9,12 → 13,10,np=5 → 13), so the padded batch still can't be chunked evenly.inference.py::pippy_forwardrelies on this to split a batch intonum_chunksforScheduleGPipe,so a non-divisible result breaks even micro-batching. The existing
test_slice_and_concatenatecasespass only because each
(batch_size, num_processes)pair happens to coincide with the correct value.Fix
Pad dim-0 up to the next multiple of
num_processes(0 when already divisible):This satisfies the docstring example (
[3,4,4], np=4 → [4,4,4]), all 8 existing assertions, andalways yields a size
>= batch_sizedivisible bynum_processes.Tests
tests/test_utils.py::test_pad_input_tensors_divisibility— checks8→8,6→8, and the invariantresult % num_processes == 0 and result >= batch_sizeacrossnum_processes∈[1,8], batch_size∈[1,29](129/232 cases were non-divisible before). Existing
test_slice_and_concatenatestill passes. CPU-only.